<?php
ob_start();
session_start();
include_once($_SERVER['DOCUMENT_ROOT'].'/htqlmba/configs/database.php');
/*

  /* Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/libraries/PHPExcel_1.7.9_doc/Classes/PHPExcel.php');

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
        ->setLastModifiedBy("Maarten Balliauw")
        ->setTitle("Office 2007 XLSX Test Document")
        ->setSubject("Office 2007 XLSX Test Document")
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
        ->setKeywords("office 2007 openxml php")
        ->setCategory("Test result file");


$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Công Ty Điện Lực Cà Mau');


$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->applyFromArray(
        array(
            'font' => array(
                'size' => 15,
                'name' => 'Times new Roman',
                'bold' => true, 'italic' => true
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
            )
        )
);
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('G1', 'CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM');
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('G2', 'Độc Lập - Tự Do - Hạnh phúc');		
		//format quốc ngữ
$objPHPExcel->getActiveSheet()->getStyle('G1:H1')->applyFromArray(
        array(
            'font' => array(
                'size' => 16,
                'name' => 'Times new Roman',
                'bold' => true
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
            )
        )
);
$objPHPExcel->getActiveSheet()->getStyle('G2:H2')->applyFromArray(
        array(
            'font' => array(
                'size' => 16,
                'name' => 'Times new Roman',
                'bold' => true
            ),
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
            )
        )
);
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A2', 'Địa chỉ: 22 Ngô Quyền P2 TP Cà Mau');
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('C2', 'Ngày Tạo: ' . date('d/m/Y', time()));
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A3', 'Liên hệ: Nguyễn Phạm Thanh Tú/CBKT/Phòng KH-KT');
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A4', 'Điện thoại/Fax/Email: ');
$objPHPExcel->getActiveSheet()->getStyle('A2:C4')->applyFromArray(
        array(
            'alignment' => array(
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
            )
        )
);

  $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A6', 'Thông tin cơ bản')
                    ->setCellValue('A7', 'Số N0')				
                    ->setCellValue('B7', 'MSTS')				
                    ->setCellValue('C7', 'Loại điện áp(U)')					
					->setCellValue('D7', 'Công Suất(KVA)')					
					->setCellValue('E7', 'Chiều dài(mm)')					
					->setCellValue('F7', 'Chiều rộng(mm)')					
					->setCellValue('G7', 'Chiều cao(mm)')					
					->setCellValue('H7', 'Đầu máy(kg)')
					->setCellValue('I7', 'Ruột máy(kg)')
					->setCellValue('J7', 'Tổng(kg)')
					->setCellValue('K7', 'Hãng sản xuất')
					->setCellValue('L7', 'Nước sản xuất')
					->setCellValue('M7', 'Loại dầu')
					->setCellValue('N7', 'Thông số đo')
					->setCellValue('O7', 'Nhận xét')
					->setCellValue('P7', 'Ghi chú');

	//format title 'thông tin cơ bản'
	  $objPHPExcel->getActiveSheet()->getStyle('A5:' .
                    $objPHPExcel->getActiveSheet()->getHighestColumn() .
                    $objPHPExcel->getActiveSheet()->getHighestRow())->applyFromArray(
                    array(
                        'font' => array(
							'bold' => true,
                            'size' => 16,							
                            'name' => 'Times new Roman'
							
                        ),           
                        'alignment' => array(
                            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                        )
                    )
            );
		
//Lấy dữ liệu cơ bản			
$db = new database();
    $db->setQuery("select * 	
					from may_bien_ap a,loai_dien_ap b,nha_sx c 
					where a.MA_DIEN_AP=b.MA_DIEN_AP and a.MA_NSX=c.MA_NSX and a.SO_N0='".$_SESSION["N0"]."'");
    $result=$db->fetchAll();	
	$row=mysql_fetch_array($result);
							
				  $objPHPExcel->setActiveSheetIndex(0)
				  	->setCellValue('A8', $row["SO_N0"])
						->setCellValue('B8', $row["MSTS"])
						->setCellValue('C8', $row["CAP_DIEN_AP"])
						->setCellValue('D8', $row["CONG_SUAT"])
						->setCellValue('E8', $row["CHIEU_DAI"])
						->setCellValue('F8', $row["CHIEU_RONG"])
						->setCellValue('G8', $row["CHIEU_CAO"])
						->setCellValue('H8', $row["DAU_MAY"])
						->setCellValue('I8', $row["RUOT_MAY"])
						->setCellValue('J8', $row["TONG"])
						->setCellValue('K8', $row["TEN_NSX"])
						->setCellValue('L8', $row["TEN_NUOC"])
						->setCellValue('M8', $row["TEN_LOAI_DAU"])
						->setCellValue('N8', $row["THONG_SO_DO"])
						->setCellValue('O8', $row["NHAN_XET"])
						->setCellValue('P8', $row["GHI_CHU"])
						->setCellValue('B11', $row["NAM_SX"]);
	
						
//format bảng thông tin cơ bản
	 $objPHPExcel->getActiveSheet()->getStyle('A7:P7')->applyFromArray(
                    array(
                        'font' => array(
                            'bold' => true,
                            'size' => 13,                            
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                            )
                        ),
                        
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
	$objPHPExcel->getActiveSheet()->getStyle('A8:P8')->applyFromArray(
                    array(	
						  'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                               
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
					)
	);			
	$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue('A10', 'Lịch sử máy biến áp')
					->setCellValue('A11', 'Năm sản xuất')
					->setCellValue('A13', 'Tình trạng')
					->setCellValue('A14', 'Ngày')
					->setCellValue('B14', 'Tình trạng')
					->setCellValue('C14', 'Diễn giải')
					->setCellValue('G13', 'Lịch sử vận hành')
					->setCellValue('G14', 'Ngày vận hành')
					->setCellValue('H14', 'Trạm');
	//format title ' Lịch sử mba'
	$objPHPExcel->getActiveSheet()->getStyle('A10:A10')->applyFromArray(
                    array(
                        'font' => array(
							'bold' => true,
                            'size' => 16,							
                            'name' => 'Times new Roman'
							
                        ),           
                        'alignment' => array(
                            'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                            'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
                        )
                    )
            );
	//format năm sx
	$objPHPExcel->getActiveSheet()->getStyle('A11:A11')->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,                           
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                               
                            )
                        ),                      
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
		$objPHPExcel->getActiveSheet()->getStyle('B11:B11')->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                              
                           	 )
                        	),
						 
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
		//Dữ liệu tình trạng		
   $count_tt=15;
   $db->setQuery("select * from chi_tiet_tt_may where SO_N0='".$_SESSION["N0"]."'");
   $result=$db->fetchAll();	
   if(mysql_num_rows($result)!=0){
	   while($row=mysql_fetch_array($result)){
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue("A".$count_tt, $row["TINH_TRANG_NGAY"])
						->setCellValue("B".$count_tt, $row["TEN_TINH_TRANG_MAY"])
						->setCellValue("C".$count_tt, $row["DG_TINH_TRANG"]);
				//tô màu dữ liệu tình trạng		
			$objPHPExcel->getActiveSheet()->getStyle("A".$count_tt.":C".$count_tt)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                                
                           	 )
                        	),
						 
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
			$count_tt++;
	   }
   }
   else{
   		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("A".$count_tt,'N/A');	
		$objPHPExcel->getActiveSheet()->mergeCells("A15:C15");
		//Tô màu ô 'N/A'
		$objPHPExcel->getActiveSheet()->getStyle("A".$count_tt)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                              
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
		$count_tt++;
   }
   	//format tình trạng
	 $objPHPExcel->getActiveSheet()->mergeCells('A13:C13');
	$objPHPExcel->getActiveSheet()->getStyle('A13:C13')->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,
                          
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                               
                            )
                        ),
                        
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
	$objPHPExcel->getActiveSheet()->getStyle('A14:C14')->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,
                            
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                               
                            )
                        ),
                      
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
   		//Dữ liệu lịch sử vận hành
	$count_lsvh=15;		
	$db->setQuery("select * from mba_tram a, tram b where a.MA_TRAM=b.MA_TRAM and SO_N0='".$_SESSION["N0"]."'");
   $result=$db->fetchAll();
   if(mysql_num_rows($result)!=0){
	   while($row=mysql_fetch_array($result)){
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue("G".$count_lsvh, $row["NGAY_VAN_HANH"])
						->setCellValue("H".$count_lsvh, $row["TEN_TRAM"]);	
			//Tô màu dữ liệu lịch sử vh
			$objPHPExcel->getActiveSheet()->getStyle("G".$count_lsvh.":H".$count_lsvh)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                               
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);	
			$count_lsvh++;
	   }
	}
    else{
   		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("G".$count_lsvh,'N/A');
		$objPHPExcel->getActiveSheet()->mergeCells('G15:H15');
		//Tô màu 'N/A'
	 	$objPHPExcel->getActiveSheet()->getStyle("G".$count_lsvh)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
		$count_lsvh++;
   }
 	//format lịch sử vận hành
	 $objPHPExcel->getActiveSheet()->mergeCells('G13:H13');
	$objPHPExcel->getActiveSheet()->getStyle('G13:H13')->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,
                           
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                            )
                        ),
                        
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
	$objPHPExcel->getActiveSheet()->getStyle('G14:H14')->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,
                           
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                              
                            )
                        ),
                      
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
   		//Dòng tiếp theo = số dòng lớn nhất giữa lịch sử và tình trạng	
	$line=0;
	if($count_tt>$count_lsvh) $line=$count_tt;
	else $line=$count_lsvh;	
		
		//Xác định vị trí bảng lịch sử ngừng vận hành và lịch sử đại tu
		$line+=1;
		
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("A".$line, 'Lịch sử ngừng vận hành')	
					->setCellValue("G".$line, 'Lịch sử đại tu');	
		
		//format lịch sử ngừng vận hành và lịch sử đại tu
		
		$objPHPExcel->getActiveSheet()->getStyle("A".$line.":A".$line)->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,
                            
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                            )
                        ),
                       
                    )
            );
			$objPHPExcel->getActiveSheet()->getStyle("G".$line.":G".$line)->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,
                           
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                            )
                        ),
                       
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
		
		$line++;
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("A".$line, 'Ngày')
					->setCellValue("G".$line, 'Ngày');	
		//format ngày
		$objPHPExcel->getActiveSheet()->getStyle("A".$line.":A".$line)->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,                            
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                            )
                        ),
                        
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
		$objPHPExcel->getActiveSheet()->getStyle("G".$line.":G".$line)->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,                           
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                              
                            )
                        ),
                        
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
		//Dữ liệu ngừng vận hành
	$count_nvh=$line+1;
   $db->setQuery("select * from chi_tiet_ngung_van_hanh where SO_N0='".$_SESSION["N0"]."'");
   $result=$db->fetchAll();	
    if(mysql_num_rows($result)!=0){
	   while($row=mysql_fetch_array($result)){
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue("A".$count_nvh, $row["NGAY_NGUNG_VH"]);	
			$objPHPExcel->getActiveSheet()->getStyle("A".$count_nvh.":A".$count_nvh)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                               
                           	 )
                        	),
						 
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);	
			$count_nvh++;
						}
	}
	else{
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("A".$count_nvh,'N/A');
		//Tô màu N/A
		$objPHPExcel->getActiveSheet()->getStyle("A".$count_nvh)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                            
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
		$count_nvh++;
	}
		//Dữ liệu Đại tu
	$count_dt=$line+1;
   $db->setQuery("select * from chi_tiet_dai_tu	where SO_N0='".$_SESSION["N0"]."'");
   $result=$db->fetchAll();
   if(mysql_num_rows($result)!=0){	   
	   while($row=mysql_fetch_array($result)){		
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue("G".$count_dt, $row["NGAY_DAI_TU"]);		
			$objPHPExcel->getActiveSheet()->getStyle("G".$count_dt.":G".$count_dt)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                              
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);	
			$count_dt++;
		}	
	}
	else{
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("G".$count_dt,'N/A');
		//Tô màu N/A
		$objPHPExcel->getActiveSheet()->getStyle("G".$count_dt)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                                
                           	 )
                        	),
						 
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
		$count_dt++;
	}
					
		//Dòng tiếp theo
	if($count_nvh>$count_dt) $line=$count_nvh;
	else $line=$count_dt;
	
		//Xác định vị trí bảng chi tiết sở hữu
	$line+=1;
	$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("A".$line, 'Chi tiết sở hữu');
	
		//merge chi tiết sở hữu	
	$objPHPExcel->getActiveSheet()->mergeCells("A".$line.":B".$line);
	//format ô 'Chi tiết sở hữu'
	$objPHPExcel->getActiveSheet()->getStyle("A".$line.":B".$line)->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,                            
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                              
                            )
                        ),
                        
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );	
		//Vi trí ngày + dơn vị
	$line++;
	$objPHPExcel->setActiveSheetIndex(0)				
					->setCellValue("A".$line, 'Ngày')	
					->setCellValue("B".$line, 'Đơn vị');	
		//format ngày + đơn vị
	$objPHPExcel->getActiveSheet()->getStyle("A".$line.":B".$line)->applyFromArray(
                    array(
                         'font' => array(
                            'bold' => true,
                            'size' => 13,                           
                            'name' => 'Times new Roman'
                        ),
                         'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,                              
                            )
                        ),
                      
						 'alignment' => array(
                		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                		'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 )
                    )
            );
		//Dữ liệu chi tiết sở hữu
	$count_ctsh=$line+1;	
	$db->setQuery("select * from chi_tiet_so_huu a, don_vi b where a.MA_DON_VI=b.MA_DON_VI and SO_N0='".$_SESSION["N0"]."'");
   $result=$db->fetchAll();
   if(mysql_num_rows($result)!=0){
   		while($row=mysql_fetch_array($result)){		
			$objPHPExcel->setActiveSheetIndex(0)
						->setCellValue("A".$count_ctsh, $row["NGAY_CHUYEN_DEN"])
						->setCellValue("B".$count_ctsh, $row["TEN_DON_VI"]);	
			$objPHPExcel->getActiveSheet()->getStyle("A".$count_ctsh.":B".$count_ctsh)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                               
                           	 )
                        	),
						 
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);		
			$count_ctsh++;
			}
	}
	else{
		$objPHPExcel->setActiveSheetIndex(0)
					->setCellValue("A".$count_ctsh,'N/A');
		$objPHPExcel->getActiveSheet()->mergeCells("A".$count_ctsh.":B".$count_ctsh);
		//Tô màu N/A
		$objPHPExcel->getActiveSheet()->getStyle("A".$count_ctsh)->applyFromArray(
                    array(	
					'font' => array(
                            'bold' => true,
                            'size' => 12,                         
                            'name' => 'Times new Roman'
							),
						  'borders' => array(
                            'allborders' => array(
                                'style' => PHPExcel_Style_Border::BORDER_THIN,
                              
                           	 )
                        	),
						
						'alignment' => array(
                			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
           				 	)
						)
		);
		$count_ctsh++;
	}

//size column
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(16);	
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(17);	
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(17);	
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(16);	
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(16);	
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(16);	
	$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(16);
	$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(16);	
	
//merge 
  $objPHPExcel->getActiveSheet()->mergeCells('A1:C1');
  $objPHPExcel->getActiveSheet()->mergeCells('G1:J1');
  $objPHPExcel->getActiveSheet()->mergeCells('G2:J2');
  $objPHPExcel->getActiveSheet()->mergeCells('A6:P6');  
  $objPHPExcel->getActiveSheet()->mergeCells('A10:P10'); 
 
 
  




// &*&*&**&* HET TEST THEM
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Kết quả tìm kiếm');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="kqtimkiem.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
?>